home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / xcdplayer / logo_setup.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  15KB  |  651 lines

  1. /*
  2.  * Copyright (C) 1990 Regents of the University of California.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of the University of
  9.  * California not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  the University of California makes no representations
  12.  * about the suitability of this software for any purpose.  It is provided
  13.  * "as is" without express or implied warranty.
  14.  */
  15.  
  16. # include <X11/Intrinsic.h>
  17. # include <X11/StringDefs.h>
  18. # include <X11/Shell.h>
  19. # include <X11/Xaw/Form.h>
  20. # include <X11/Xaw/Label.h>
  21. # include <X11/Xaw/Command.h>
  22. # include <X11/Xaw/Toggle.h>
  23. # include <X11/Xaw/Scrollbar.h>
  24. # include <X11/Xaw/Dialog.h>
  25. # include <X11/Xaw/Viewport.h>
  26.  
  27. # include <math.h>
  28. # include <stdio.h>
  29.  
  30. # include "cdrom_globs.h"
  31. #ifdef sun
  32. # include "cdrom_sun.h"
  33. #endif
  34. #ifdef sgi
  35. # include "cdrom_sgi.h"
  36. #endif
  37.  
  38. # include "logo.xbm"
  39. # include "thumb.xbm"
  40. # include <X11/bitmaps/gray>
  41.  
  42. # define MAXVOL        0xff
  43.  
  44. void    Done();
  45.  
  46. static Widget    track_button_widget;
  47. static Widget    timer_button_widget;
  48. static Widget    volume_scroll_widget;
  49.  
  50. Widget        title_viewport;
  51. Widget        title_form;
  52. Widget        title_button;
  53. Widget        title_dialog_shell;
  54. Widget        title_dialog_widget;
  55. Widget        title_done_widget;
  56.  
  57. static int    vol;
  58.  
  59. void
  60. logo_setup(parent_widget)
  61.     Widget        parent_widget;
  62. {
  63.     extern void    leds_label_setup();
  64.     extern void    cb_track_button();
  65.     extern void    cb_timer_button();
  66.     extern void    volume_jump_proc();
  67.     extern void    volume_scroll_proc();
  68.     extern void    popup_title_dialog();
  69.     extern void    popdown_title_dialog();
  70.  
  71.     Widget        version_label_widget;
  72.     char        version_string[80];
  73.  
  74.     Widget        logo_form_widget;
  75.     Widget        logo_label_widget;
  76.     Pixmap        logo_label_pixmap;
  77.     Pixmap        thumb_pixmap;
  78.     Pixmap        gray;
  79.     int            depth;
  80.     int        length;
  81.     
  82.  
  83.     Arg        args[4];
  84.     static XtActionsRec    done[] = {
  85.         {"done", Done},
  86.     };
  87.  
  88.  
  89.     logo_form_widget = XtCreateManagedWidget("logoForm", formWidgetClass,
  90.                          parent_widget,
  91.                          (ArgList) NULL, 0);
  92.  
  93.     logo_label_widget = XtCreateManagedWidget("logoLabel",
  94.                           labelWidgetClass,
  95.                           logo_form_widget,
  96.                           (ArgList) NULL, 0);
  97.  
  98.     logo_label_pixmap = XCreateBitmapFromData(XtDisplay(logo_label_widget),
  99.                           rootwin(logo_label_widget),
  100.                           logo_bits,
  101.                           logo_width, logo_height);
  102.  
  103.     sprintf(version_string,"XCdplayer %s%d",VERSION,PATCHLEVEL);
  104.     if (BETA != 0) {
  105.         sprintf(version_string,"%s beta %d",version_string,BETA);
  106.     }
  107.     XtSetArg(args[0], XtNjustify, XtJustifyRight);
  108.     XtSetArg(args[1], XtNlabel, version_string);
  109.     version_label_widget = XtCreateManagedWidget("versionLabel",
  110.                              labelWidgetClass,
  111.                              logo_form_widget,
  112.                              (ArgList) args, 2);
  113.  
  114.     XtSetArg(args[0], XtNbitmap, logo_label_pixmap);
  115.     XtSetValues(logo_label_widget, args, 1);
  116.  
  117.     leds_label_setup(logo_form_widget);
  118.  
  119.  
  120.     track_button_widget = XtCreateManagedWidget("trackButton",
  121.                             toggleWidgetClass,
  122.                             logo_form_widget,
  123.                             (ArgList) NULL, 0);
  124.  
  125.     XtAddCallback(track_button_widget, XtNcallback, cb_track_button, 0);
  126.  
  127.     track_button_update();
  128.  
  129.     if (display_timer == True)
  130.         timer_button_widget = XtCreateManagedWidget("timerButton",
  131.                                    toggleWidgetClass,
  132.                                logo_form_widget,
  133.                                (ArgList) NULL, 0);
  134.  
  135.     XtAddCallback(timer_button_widget, XtNcallback, cb_timer_button, 0);
  136.  
  137.     timer_button_update();
  138.     thumb_pixmap = XCreateBitmapFromData(XtDisplay(logo_form_widget),
  139.                           rootwin(logo_form_widget),
  140.                           thumb_bits,
  141.                           thumb_width, thumb_height);
  142.  
  143.     XtSetArg (args[0], XtNdepth, &depth);
  144.     XtGetValues (logo_form_widget, args, 1);
  145.  
  146.     if (depth == 1) {
  147.         gray = XCreateBitmapFromData(XtDisplay(logo_form_widget),
  148.                           rootwin(logo_form_widget),
  149.                           gray_bits,
  150.                           gray_width, gray_height);
  151.  
  152.         XtSetArg(args[0], XtNbackgroundPixmap, gray);
  153.         XtSetArg(args[1], XtNthumb, thumb_pixmap);
  154.         volume_scroll_widget = XtCreateManagedWidget("volumeScrollbar",
  155.                              scrollbarWidgetClass,
  156.                              logo_form_widget,
  157.                              (ArgList) args, 2);
  158.     }
  159.     else
  160.     {
  161.          /*XtSetArg(args[0], XtNthumb, thumb_pixmap);*/
  162.  
  163.          volume_scroll_widget = XtCreateManagedWidget("volumeScrollbar",
  164.                              scrollbarWidgetClass,
  165.                              logo_form_widget,
  166.                              (ArgList) args, 0);
  167.     }
  168.  
  169.     XtAddCallback(volume_scroll_widget, XtNjumpProc, volume_jump_proc, 0);
  170.     XtAddCallback(volume_scroll_widget, XtNscrollProc, volume_scroll_proc, 0);
  171.  
  172. #ifdef sgi
  173.     if ((vol = cdrom_get_volume()) == 0) {
  174.         vol = (int) ((MAXVOL - volbase) * 0.75) + volbase;
  175.     }
  176. #else
  177.     vol = (int) ((MAXVOL - volbase) * 0.75) + volbase;
  178. #endif
  179.     cdrom_volume(vol, vol); 
  180. #ifdef sgi
  181.     XawScrollbarSetThumb(volume_scroll_widget, 
  182.                  (float) (VAL2PCT(vol)),
  183.                  (float) 1.0);
  184. #else
  185.     XawScrollbarSetThumb(volume_scroll_widget, (float) 0.75, (float) 1.0);
  186. #endif
  187.  
  188.     title_viewport = XtCreateManagedWidget ("titleBarViewport",
  189.                 viewportWidgetClass,
  190.                 logo_form_widget,
  191.                 (ArgList) NULL, 0);
  192.  
  193.     title_form = XtCreateManagedWidget ("titleForm",
  194.                 formWidgetClass,
  195.                 title_viewport,
  196.                 (ArgList) NULL, 0);
  197.  
  198.     if (disc_title != NULL) 
  199.             XtSetArg(args[0], XtNlabel, disc_title);
  200.     else
  201. #ifdef sgi
  202.         if (cdrom_status() == CDROM_NO_STATUS) {
  203.             XtSetArg(args[0], XtNlabel, NODISCSTR);
  204.         } else {
  205.             XtSetArg(args[0], XtNlabel, NOTITLESTR);
  206.         }
  207. #else
  208.         XtSetArg(args[0], XtNlabel, NOTITLESTR);
  209. #endif
  210.  
  211.     title_button = XtCreateManagedWidget ("titleBar",
  212.                 commandWidgetClass,
  213.                 title_form,
  214.                 (ArgList) args, 1);
  215.  
  216.     title_dialog_shell = XtCreatePopupShell("titleShell", 
  217.                 transientShellWidgetClass,
  218.                 title_button,
  219.                 (ArgList) NULL, 0);
  220.  
  221.         XtSetArg(args[0], XtNlabel, "Title: ");
  222.         title_dialog_widget = XtCreateManagedWidget("titleDialog",
  223.                                     dialogWidgetClass,
  224.                                     title_dialog_shell,
  225.                                     (ArgList) args, 1);
  226.  
  227.     title_done_widget = XtCreateManagedWidget("Done",
  228.                     commandWidgetClass,
  229.                     title_dialog_widget,
  230.                     (ArgList) NULL, 0);
  231.     
  232.     XtAppAddActions(appc, done, XtNumber(done));
  233.  
  234.     XtAddCallback(title_button, XtNcallback, popup_title_dialog,
  235.             (XtPointer) logo_form_widget);
  236.     XtAddCallback(title_done_widget, XtNcallback, popdown_title_dialog,
  237.             (XtPointer) title_dialog_widget);
  238.  
  239.  
  240. }
  241.  
  242. /*ARGSUSED*/
  243. static void
  244. cb_track_button(widget, client_data, call_data)
  245.     Widget        widget;
  246.     XtPointer    client_data;
  247.     XtPointer    call_data;
  248. {
  249.     char        track_buf[40];
  250.     Arg        args[1];
  251.     Boolean        state;
  252.  
  253.     XtSetArg(args[0], XtNstate, &state);
  254.     XtGetValues(widget, args, 1);
  255.  
  256. #ifdef sgi
  257.     if (cdrom_status() == CDROM_NO_STATUS) {
  258.         cdi.state |= CDROM_STATE_EJECTED;
  259.         buttons_reset();
  260.         return;
  261.     }
  262. #endif
  263.  
  264.     if (cdi.state & CDROM_STATE_EJECTED) {
  265.         cdrom_new_disc();
  266.     }
  267.  
  268.     if (state == True)
  269.         sprintf(track_buf, "%d\n", cdi.maxtrack);
  270.     else
  271.         sprintf(track_buf, "%d\n", cdi.curtrack);
  272.  
  273.     XtSetArg(args[0], XtNlabel, track_buf);
  274.     XtSetValues(widget, args, 1);
  275. }
  276.  
  277. void
  278. track_button_update() {
  279.     char        track_buf[40];
  280.     Arg        args[1];
  281.  
  282.     sprintf(track_buf, "%d\n", cdi.curtrack);
  283.  
  284.     XtSetArg(args[0], XtNlabel, track_buf);
  285.     XtSetValues(track_button_widget, args, 1);
  286. }
  287.  
  288. static void
  289. cb_timer_button(widget, client_data, call_data)
  290.     Widget        widget;
  291.     XtPointer    client_data;
  292.     XtPointer    call_data;
  293. {
  294.     char        timer_buf[40];
  295.     int        time_remaining;
  296.     Arg        args[1];
  297.     Boolean        state;
  298.  
  299.     if (cdi.state & CDROM_STATE_EJECTED) {
  300.         cdrom_new_disc();
  301.     }
  302.  
  303. #ifdef sgi
  304.     if (cdrom_status() == CDROM_NO_STATUS) {
  305.         cdi.state |= CDROM_STATE_EJECTED;
  306.         buttons_reset();
  307.         return;
  308.     }
  309. #endif
  310.  
  311.     cdi.duration = cdrom_get_curtime();
  312.  
  313.     XtSetArg(args[0], XtNstate, &state);
  314.     XtGetValues(widget, args, 1);
  315.  
  316.     if ((state == True) && (cdi.curtrack != 0))
  317.     {
  318.         if (((cdi.state & CDROM_STATE_PAUSE) == 0) &&
  319.             (cdi.state & CDROM_STATE_PROGRAM))
  320.         {
  321.             time_remaining = program_time_remaining();
  322.         }
  323.         else
  324.             time_remaining = cdi.times[cdi.curtrack - 1] - cdi.duration;
  325.  
  326.         if (time_remaining < 0)
  327.             sprintf(timer_buf, "-%02u:%02u\n",abs(time_remaining) / 60, 
  328.             abs(time_remaining) % 60);
  329.         else
  330.             sprintf(timer_buf, "-%02d:%02d\n", time_remaining / 60, 
  331.             time_remaining % 60);
  332.     } else if (cdi.curtrack != 0) {
  333.         if (cdi.duration < 0) 
  334.             sprintf(timer_buf, "-%02u:%02u\n", cdi.duration / 60, 
  335.                 abs(cdi.duration) % 60);
  336.         else
  337.             sprintf(timer_buf, "%02u:%02u\n", cdi.duration / 60, 
  338.                 cdi.duration % 60);
  339.     } else {
  340.         if ((state == True) && (cdrom_status() != CDROM_NO_STATUS))
  341.             sprintf(timer_buf, "-%02u:%02u\n", 
  342.                 cdi.addrs[cdi.maxtrack].minute, 
  343.                 cdi.addrs[cdi.maxtrack].second);
  344.         else
  345.             sprintf(timer_buf, "--:--\n");
  346.     }
  347.  
  348.     XtSetArg(args[0], XtNlabel, timer_buf);
  349.     XtSetValues(widget, args, 1);
  350. }
  351.  
  352. void
  353. timer_button_update() {
  354.     char        timer_buf[40];
  355.     int        time_remaining;
  356.     Arg        args[1];
  357.     Boolean        state;
  358.  
  359.     if (display_timer == False)
  360.         return;
  361.  
  362.         if ((cdi.state & CDROM_STATE_PAUSE) == 0)
  363.             cdi.duration = cdrom_get_curtime();
  364.  
  365.         XtSetArg(args[0], XtNstate, &state);
  366.         XtGetValues(timer_button_widget, args, 1);
  367.  
  368.         if ((state == True) && (cdi.curtrack != 0))
  369.         {
  370.         if (((cdi.state & CDROM_STATE_PAUSE) == 0) &&
  371.             (cdi.state & CDROM_STATE_PROGRAM))
  372.         {
  373.                 time_remaining = program_time_remaining();
  374.         }
  375.         else
  376.                 time_remaining = cdi.times[cdi.curtrack - 1] - cdi.duration;
  377.             if (time_remaining < 0)
  378.                 sprintf(timer_buf, "-%02u:%02u\n",abs(time_remaining) / 60, 
  379.                 abs(time_remaining) % 60);
  380.             else
  381.                 sprintf(timer_buf, "-%02d:%02d\n", time_remaining / 60, 
  382.                 time_remaining % 60);
  383.         }
  384.         else if (cdi.state & CDROM_STATE_PLAY)
  385.         {
  386.             if (cdi.duration < 0)
  387.                 sprintf(timer_buf, "-%02u:%02u\n", cdi.duration / 60, 
  388.                 abs(cdi.duration) % 60);
  389.             else
  390.                 sprintf(timer_buf, "%02u:%02u\n", cdi.duration / 60, 
  391.                 cdi.duration % 60);
  392.         }
  393.         else
  394.         if ((state == True) && (cdrom_status() != CDROM_NO_STATUS))
  395.             sprintf(timer_buf, "-%02u:%02u\n", 
  396.                 cdi.addrs[cdi.maxtrack].minute, 
  397.                 cdi.addrs[cdi.maxtrack].second);
  398.         else
  399.             sprintf(timer_buf, "--:--\n");
  400.  
  401.     XtSetArg(args[0], XtNlabel, timer_buf);
  402.     XtSetValues(timer_button_widget, args, 1);
  403. }
  404.  
  405. static void
  406. volume_jump_proc(scroll_widget, client_data, percent)
  407.     Widget        scroll_widget;
  408.     XtPointer    client_data;
  409.     XtPointer    percent;
  410. {
  411. #ifdef sgi
  412.     /* The volume control on SGI is not linear, but is exponential */
  413.     vol = PCT2VAL(*(float *)percent);
  414. #else
  415.     vol = (*(float *) percent) * MAXVOL;
  416.     vol = (vol * volpcent) + volbase;
  417. #endif
  418.  
  419.     if (vol > MAXVOL)
  420.         vol = MAXVOL;
  421.     if (vol <= 0)
  422.         vol = 1;
  423.  
  424.     debug_printf(1, "volume=%u\n", (unsigned int) vol);
  425.  
  426.     cdrom_volume(vol, vol);
  427. }
  428.  
  429. static void
  430. volume_scroll_proc(scroll_widget, client_data, position)
  431.     Widget        scroll_widget;
  432.     XtPointer    client_data;
  433.     XtPointer    position;
  434. {
  435.     Arg        args[1];
  436.     Dimension    length;
  437.     float        top;
  438.     double        abspos;
  439.     double        percent;
  440.     double        vdelta;
  441.  
  442.     if ((abspos = (int) position) < 0)
  443.         abspos = -abspos;
  444.  
  445.     XtSetArg(args[0], XtNlength, &length);
  446.     XtGetValues(scroll_widget, args, 1);
  447.  
  448.     if (length <= 0)
  449.         length = 1;
  450.  
  451.     percent = abspos / (float) length;
  452.  
  453. #ifdef sgi
  454.     /* The volume control on SGI is not linear, but is exponential */
  455.     if ((vdelta = ((VAL2PCT(vol)) * percent)) < (VAL2PCT(1))) {
  456.         vdelta = (VAL2PCT(1));
  457.     }
  458.     if ((int) position < 0)
  459.         vol = (PCT2VAL((VAL2PCT(vol)) - vdelta));
  460.     else
  461.         vol = (PCT2VAL((VAL2PCT(vol)) + vdelta));
  462. #else
  463.     if ((vdelta = (vol * percent)) < 1.0)
  464.         vdelta = 1.0;
  465.  
  466.     if ((int) position < 0)
  467.         vol += vdelta;
  468.     else
  469.         vol -= vdelta;
  470. #endif
  471.  
  472.     if (vol > MAXVOL)
  473.         vol = MAXVOL;
  474.     if (vol <= 0)
  475.         vol = 1;
  476.  
  477.     debug_printf(1, "volume=%u\n", (unsigned int) vol);
  478.  
  479.     cdrom_volume(vol, vol);
  480.  
  481. #ifdef sgi
  482.     XtSetArg (args[0], XtNlength, &length);
  483.     XtGetValues (volume_scroll_widget, args, 1);
  484.     XawScrollbarSetThumb(volume_scroll_widget, 
  485.                  (float) (VAL2PCT(vol)),
  486.                  (float) -1.0);
  487. #endif
  488.     top = (double) vol / (double) MAXVOL;
  489.  
  490.     XawScrollbarSetThumb(volume_scroll_widget, (float) top, (float) -1.0);
  491. }
  492.  
  493. void
  494. track_button_set() {
  495.     Arg             args[1];
  496.  
  497.     XtSetArg(args[0], XtNstate, True);
  498.     XtSetValues(track_button_widget, args, 1);
  499. }
  500.  
  501. void
  502. track_button_reset() {
  503.     Arg             args[1];
  504.  
  505.     XtSetArg(args[0], XtNstate, False);
  506.     XtSetValues(track_button_widget, args, 1);
  507. }
  508.  
  509. void
  510. timer_button_set() {
  511.     Arg             args[1];
  512.  
  513.     XtSetArg(args[0], XtNstate, True);
  514.     XtSetValues(timer_button_widget, args, 1);
  515. }
  516.  
  517. void
  518. timer_button_reset() {
  519.     Arg             args[1];
  520.  
  521.     XtSetArg(args[0], XtNstate, False);
  522.     XtSetValues(timer_button_widget, args, 1);
  523. }
  524.  
  525.  
  526. /*ARGSUSED*/
  527. void
  528. popup_title_dialog(widget, parent, call_data)
  529. Widget        widget;
  530. Widget        parent;
  531. XtPointer    call_data;
  532. {
  533.     Position x, y;
  534.     Dimension width, height;
  535.     Arg    args[2];
  536.     Widget    value;
  537.     char    non_null[256];
  538.  
  539.  
  540.  
  541.     if (disc_title != NULL) {
  542.         sscanf(disc_title, "%s", non_null);
  543.  
  544.         if (strcmp ("", non_null) != 0)
  545.                 XtSetArg(args[0], XtNvalue, disc_title);
  546.         else
  547.                 XtSetArg(args[0], XtNvalue, "");
  548.     }
  549.     else
  550.             XtSetArg(args[0], XtNvalue, "");
  551.  
  552.     XtSetValues(title_dialog_widget, args, 1);    
  553.  
  554.     XtTranslateCoords (widget, 
  555.         (Position) 14,
  556.         (Position) 14,
  557.         &x, &y);
  558.     
  559.     XtSetArg(args[0], XtNx, x);
  560.     XtSetArg(args[1], XtNy, y);
  561.     XtSetValues(title_dialog_shell, args, 2);
  562.  
  563.     if ((value = XtNameToWidget(title_dialog_widget, "value")) != 0) {
  564.         XtSetArg(args[0], XtNwidth, &width);
  565.         XtGetValues(title_button, args, 1);
  566.         XtSetArg(args[0], XtNwidth, width);
  567.         XtSetValues(value, args, 1);
  568.     }
  569.  
  570.     /*XtSetSensitive (widget, FALSE); */
  571.     XtPopup (title_dialog_shell, XtGrabNone);
  572.  
  573. }
  574.  
  575. void
  576. Done(widget, event, params, num_params)
  577.     Widget        widget;
  578.     XButtonEvent    *event;
  579.     String        *params;
  580.     Cardinal    *num_params;
  581. {
  582.     Arg    args[2];
  583.  
  584.     if (disc_title != NULL)
  585.         free(disc_title);
  586.     disc_title = XawDialogGetValueString(title_dialog_widget);
  587.     XtSetArg(args[0], XtNlabel, disc_title);
  588.     XtSetValues(title_button, args, 1);
  589.  
  590.     XtPopdown(title_dialog_shell);
  591.     XtSetSensitive(title_button, TRUE);
  592. }
  593.  
  594. void
  595. popdown_title_dialog(widget, dialog, call_data)
  596. Widget    widget;
  597. Widget    dialog;
  598. XtPointer call_data;
  599. {
  600.     Arg    args[2];
  601.  
  602.     if (disc_title != NULL)
  603.         free(disc_title);
  604.     disc_title = XawDialogGetValueString(dialog);
  605.     XtSetArg(args[0], XtNlabel, disc_title);
  606.     XtSetValues(title_button, args, 1);
  607.  
  608.     XtPopdown(title_dialog_shell);
  609.     XtSetSensitive(title_button, TRUE);
  610. }
  611.  
  612. void
  613. update_title () {
  614.     Arg        args[1];
  615.     char    non_null[256];
  616.  
  617.     if (title_button != NULL) {
  618. #if 0
  619.         if (disc_title != NULL) {
  620.             sscanf(disc_title, "%s", non_null);
  621.             if (strcmp ("", non_null) != 0)
  622.                 XtSetArg(args[0], XtNvalue, disc_title);
  623.             else
  624.                 XtSetArg(args[0], XtNvalue, "");
  625.         else
  626.             XtSetArg(args[0], XtNvalue, "");
  627.         }
  628.  
  629.         XtSetArg(args[0], XtNvalue, disc_title);
  630.         XtSetValues(title_button, args, 1);
  631.         XtSetArg(args[0], XtNvalue, disc_title);
  632.         XtSetValues(title_dialog_widget, args, 1);
  633. #endif
  634.         XtSetArg(args[0], XtNvalue, disc_title);
  635.         XtSetValues(title_dialog_widget, args, 1);
  636.         XtSetArg(args[0], XtNlabel, disc_title);
  637.         XtSetValues(title_button, args, 1);
  638.  
  639.     }
  640. }
  641.  
  642. #ifdef sgi
  643. set_volume(vol) 
  644.     int    vol;
  645. {
  646.     XawScrollbarSetThumb(volume_scroll_widget, 
  647.                  (float) (VAL2PCT(vol)),
  648.                  (float) 1.0);
  649. }
  650. #endif
  651.